package com.imicode.hotel.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.imicode.hotel.dao.FoodTypeDao;
import com.imicode.hotel.utils.Dbutils;
import com.imicode.hotel.vo.FoodType;

public class FoodTypeDaoImpl implements FoodTypeDao {

	final static String ADDSQL = "insert into foodtype(typeName) values(?)";
	final static String LISTSQL = "select id,typeName from foodtype";
	final static String GETSQL = "select id,typeName from foodtype where id=?";
	final static String EDITSQL = "update foodtype set typeName=? where id=?";
	final static String DELETESQL = "delete from foodtype where id=?";

	@Override
	public void save(String name) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 1. 连接数据库
			conn = Dbutils.getConnection();
			// 2. 执行sql
			pstmt = conn.prepareStatement(ADDSQL);
			pstmt.setString(1, name);
			int row = pstmt.executeUpdate();
			// 3. 处理返回结果
			System.out.println("-----------------row: " + row);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dbutils.close(conn, pstmt, null);
		}

	}
	
	@Override
	public List<FoodType> list(String name) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<FoodType> list = new ArrayList<>();
		try {
			// 1. 连接数据库
			conn = Dbutils.getConnection();
			// 2. 执行sql
			pstmt = conn.prepareStatement(LISTSQL);
			
			rs = pstmt.executeQuery();
			// 3. 处理返回结果
			if(rs != null) {
				while(rs.next()) {
					FoodType foodType = new FoodType();
					foodType.setId(rs.getInt("id"));
					foodType.setTypeName(rs.getString("typeName"));
					list.add(foodType);
				}
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dbutils.close(conn, pstmt, rs);
		}
		
		
		return list;
	}
	
	@Override
	public FoodType get(int id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		FoodType foodType = null;
		try {
			// 1. 连接数据库
			conn = Dbutils.getConnection();
			// 2. 执行sql
			pstmt = conn.prepareStatement(GETSQL);
			pstmt.setInt(1, id);
			
			rs = pstmt.executeQuery();
			// 3. 处理返回结果
			if(rs.next()) {
				foodType = new FoodType();
				foodType.setId(rs.getInt("id"));
				foodType.setTypeName(rs.getString("typeName"));
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dbutils.close(conn, pstmt, rs);
		}
		
		
		return foodType;
	}
	
	@Override
	public void edit(int id,String name) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 1. 连接数据库
			conn = Dbutils.getConnection();
			// 2. 执行sql
			pstmt = conn.prepareStatement(EDITSQL);
			pstmt.setString(1, name);
			pstmt.setInt(2, id);
			int row = pstmt.executeUpdate();
			System.out.println(row);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dbutils.close(conn, pstmt, null);
		}
	}
	
	@Override
	public void delete(int id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 1. 连接数据库
			conn = Dbutils.getConnection();
			// 2. 执行sql
			pstmt = conn.prepareStatement(DELETESQL);
			pstmt.setInt(1, id);
			int row = pstmt.executeUpdate();
			System.out.println(row);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dbutils.close(conn, pstmt, null);
		}
	}

}
